﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Security.Claims;
using System.Security.Principal;
using System.Threading;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MARGYE.Web
{
    public partial class SiteMaster : MasterPage
    {
        private const string AntiXsrfTokenKey = "__AntiXsrfToken";
        private const string AntiXsrfUserNameKey = "__AntiXsrfUserName";
        private string _antiXsrfTokenValue;

        
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            //var IT = langItalian.Checked;
            //var EN = langEnglish.Checked;
            
            //if ((!IT) & (!EN))
            //{
            //    var culture = CultureInfo.InstalledUICulture.IetfLanguageTag;

            //    if (HttpContext.Current != null && HttpContext.Current.Request.UserLanguages != null)
            //    {
            //        culture = Request.UserLanguages[0];
            //    }

            //    if (culture.Contains("it")) IT = true;
            //    if (culture.Contains("en")) EN = true;
            //}
            
            //{
            //    LanguageHidden.Value = (IT ? "it" : "en");
            //    if (IT)
            //        langItalian.Checked = true;
            //    else
            //        langEnglish.Checked = true;
            //}

            //HttpCookie cookie = new HttpCookie("LANGUAGE");
            //cookie.Value = LanguageHidden.Value;
            //Response.SetCookie(cookie);
            
        }    

        protected void Page_Init(object sender, EventArgs e)
        {


            

            // Il codice seguente facilita la protezione da attacchi XSRF
            var requestCookie = Request.Cookies[AntiXsrfTokenKey];
            Guid requestCookieGuidValue;
            if (requestCookie != null && Guid.TryParse(requestCookie.Value, out requestCookieGuidValue))
            {
                // Utilizzare il token Anti-XSRF dal cookie
                _antiXsrfTokenValue = requestCookie.Value;
                Page.ViewStateUserKey = _antiXsrfTokenValue;
            }
            else
            {
                // Generare un nuovo token Anti-XSRF e salvarlo nel cookie
                _antiXsrfTokenValue = Guid.NewGuid().ToString("N");
                Page.ViewStateUserKey = _antiXsrfTokenValue;

                var responseCookie = new HttpCookie(AntiXsrfTokenKey)
                {
                    HttpOnly = true,
                    Value = _antiXsrfTokenValue
                };
                if (FormsAuthentication.RequireSSL && Request.IsSecureConnection)
                {
                    responseCookie.Secure = true;
                }
                Response.Cookies.Set(responseCookie);
            }

            Page.PreLoad += master_Page_PreLoad;
        }

        protected void master_Page_PreLoad(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Impostare il token Anti-XSRF
                ViewState[AntiXsrfTokenKey] = Page.ViewStateUserKey;
                ViewState[AntiXsrfUserNameKey] = Context.User.Identity.Name ?? String.Empty;
            }
            else
            {
                // Convalidare il token Anti-XSRF
                if ((string)ViewState[AntiXsrfTokenKey] != _antiXsrfTokenValue
                    || (string)ViewState[AntiXsrfUserNameKey] != (Context.User.Identity.Name ?? String.Empty))
                {
                    throw new InvalidOperationException("Convalida del token Anti-XSRF non riuscita.");
                }
            }
        }

       

        protected void Unnamed_LoggingOut(object sender, LoginCancelEventArgs e)
        {
            Context.GetOwinContext().Authentication.SignOut();
        }

        protected void lang_CheckedChanged(object sender, EventArgs e)
        {
            var name = (sender as CheckBox).ID;
            var lang =( langItalian.Checked ? "it" : "en" );
            LanguageHidden.Value = lang;

            //Thread.CurrentThread.CurrentCulture = new CultureInfo(lang);
            //Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);

            HttpCookie cookie = new HttpCookie("LANGUAGE");
            cookie.Value = lang;
            Response.SetCookie(cookie);

            if (lang == "it")
                langItalian.Checked = true;
            else
                langEnglish.Checked = true;


            var url = Request.Url.OriginalString.Contains("lang")? 
                Request.Url.OriginalString :  Request.Url.OriginalString + "?lang="+ lang;
            Response.Redirect(url , false);

        }

    }

}